package com.pengtu.service.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.business.CompExpertiseDao;
import com.pengtu.dao.business.CompPetitionDao;
import com.pengtu.dao.business.CompReportDao;
import com.pengtu.dao.business.FlowCompletionDao;
import com.pengtu.dao.quality.FlowProjectDao;
import com.pengtu.entity.app.User;
import com.pengtu.entity.business.check.CompExpertise;
import com.pengtu.entity.business.check.CompPetition;
import com.pengtu.entity.business.check.CompReport;
import com.pengtu.entity.business.check.FlowCompletion;
import com.pengtu.entity.history.HistoryProject;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.entity.quality.FlowProject;
import com.pengtu.service.BaseManager;
import com.pengtu.service.history.HistoryProjectManager;
import com.pengtu.service.project.ProjectManager;

@Component
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class CompletionManager extends BaseManager {
	private CompExpertiseDao compExpertiseDao;
	private CompPetitionDao compPetitionDao;
	private CompReportDao compReportDao;
	private FlowCompletionDao flowCompletionDao;
	private FlowProjectDao flowProjectDao;

	private HistoryProjectManager historyProjectManager;
	private ProjectManager projectManager;

	@Autowired
	public void setCompExpertiseDao(CompExpertiseDao compExpertiseDao) {
		this.compExpertiseDao = compExpertiseDao;
	}

	@Autowired
	public void setCompPetitionDao(CompPetitionDao compPetitionDao) {
		this.compPetitionDao = compPetitionDao;
	}

	@Autowired
	public void setCompReportDao(CompReportDao compReportDao) {
		this.compReportDao = compReportDao;
	}

	@Autowired
	public void setFlowCompletionDao(FlowCompletionDao flowCompletionDao) {
		this.flowCompletionDao = flowCompletionDao;
	}

	@Autowired
	public void setHistoryProjectManager(
			HistoryProjectManager historyProjectManager) {
		this.historyProjectManager = historyProjectManager;
	}

	@Autowired
	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

	@Autowired
	public void setFlowProjectDao(FlowProjectDao flowProjectDao) {
		this.flowProjectDao = flowProjectDao;
	}

	/**
	 * 启动流程 start: 适用:
	 * 
	 * @param flowCompletion
	 * @param deploymentId
	 * @param user
	 * @param projectInfo
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public void start(FlowCompletion flowCompletion, String deploymentId,
			User user, ProjectInfo projectInfo) {
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put(Constants.COMPLE_ID, flowCompletion.getId());
		super.start(flowCompletion.getId(), deploymentId,
				"comple", variables);
		flowCompletionDao.save(flowCompletion);
		projectManager.saveOrupdateProj(projectInfo);
	}

	/**
	 * 保存 save: 适用:
	 * 
	 * @param flowCompletion
	 * @param historyProject
	 * @exception
	 * @since 1.0.0
	 */
	public void save(FlowCompletion flowCompletion,
			HistoryProject historyProject) {
		flowCompletionDao.save(flowCompletion);
		historyProject.setKey(flowCompletion.getId());
		historyProjectManager.save(historyProject);
	}

	public void save(FlowCompletion flowCompletion) {
		flowCompletionDao.save(flowCompletion);
	}

	public void save(CompExpertise compExpertise) {
		compExpertiseDao.save(compExpertise);
	}

	public void save(CompPetition compPetition) {
		compPetitionDao.save(compPetition);
	}

	/**
	 * 保存流程表和申请书 save: 适用:
	 * 
	 * @param compPetition
	 * @param flowCompletion
	 * @exception
	 * @since 1.0.0
	 */
	public void save(CompPetition compPetition, FlowCompletion flowCompletion) {
		flowCompletionDao.save(flowCompletion);
		compPetition.setFlowCompletion(flowCompletion);
		compPetitionDao.save(compPetition);
	}

	public void save(CompReport compReport) {
		compReportDao.save(compReport);
	}

	/**
	 * 接受任务 takeTask: 适用:
	 * 
	 * @param taskId
	 * @param key
	 * @param user
	 * @param historyProject
	 * @exception
	 * @since 1.0.0
	 */
	public void takeTask(String taskId, String key, User user,
			HistoryProject historyProject) {
		super.takeTask(taskId, user);
		historyProject.setKey(key);
		historyProjectManager.save(historyProject);
	}

	/**
	 * 查询待办任务 findFlowCompletions: 适用:
	 * 
	 * @param user
	 * @param projectInfo
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public List<FlowCompletion> findFlowCompletions(User user,
			ProjectInfo projectInfo) {
		List<FlowCompletion> list = new ArrayList<FlowCompletion>();
		FlowCompletion flowCompletion = this.findGroupTasks(user, projectInfo);
		List<FlowCompletion> fCompletions = this.findPersonalTasks(user,
				projectInfo);
		if (flowCompletion != null) {
			list.add(flowCompletion);
		}
		if (fCompletions != null) {
			for (FlowCompletion flowCompletion2 : fCompletions) {
				list.add(flowCompletion2);
			}
		}
		return list;
	}

	public FlowCompletion findGroupTasks(User user, ProjectInfo projectInfo) {
		FlowCompletion flowCompletion = null;
		List<Task> taskList = super.findGroupTasks(user);
		for (Task task : taskList) {
			String taskId = task.getId();
			String exectionId = task.getExecutionId();
			String arg = exectionId.substring(0, 6);
			if ("comple".equals(arg)) {
				String id = (String) super.getVariableByTaskId(taskId,
						Constants.COMPLE_ID);
				flowCompletion = flowCompletionDao.get(id);
				if (flowCompletion != null
						& flowCompletion.getProjectInfo().getId()
								.equals(projectInfo.getId())) {
					flowCompletion.setTaskId(taskId);
				}
			}
		}
		return flowCompletion;
	}

	public List<FlowCompletion> findPersonalTasks(User user,
			ProjectInfo projectInfo) {
		List<FlowCompletion> flowCompletions = new ArrayList<FlowCompletion>();
		List<Task> taskList = super.findPersonalTasks(user);
		for (Task task : taskList) {
			String taskId = task.getId();
			String exectionId = task.getExecutionId();
			String arg = exectionId.substring(0, 6);
			if ("comple".equals(arg)) {
				String id = (String) super.getVariableByTaskId(taskId,
						Constants.COMPLE_ID);
				FlowCompletion flowCompletion = flowCompletionDao.get(id);
				if (flowCompletion != null
						& flowCompletion.getProjectInfo().getId()
								.equals(projectInfo.getId())) {
					flowCompletion.setTaskId(taskId);
					flowCompletions.add(flowCompletion);
				}
			}
		}
		return flowCompletions;
	}

	/**
	 * 判断是否待办任务 isPersonalTasks: 适用:
	 * 
	 * @param user
	 * @param id
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public String isPersonalTasks(User user, String id) {
		return super.isPersonalTasks(user, id, Constants.COMPLE_ID);
	}

	/**
	 * 任务提交 completeTask: 适用:
	 * 
	 * @param flowCompletion
	 * @param outcome
	 * @param variables
	 * @param historyProject
	 * @param projectInfo
	 * @exception
	 * @since 1.0.0
	 */
	public void completeTask(FlowCompletion flowCompletion, String outcome,
			Map<String, Object> variables, HistoryProject historyProject,
			ProjectInfo projectInfo) {
		super.completeTask(flowCompletion.getTaskId(), outcome, variables);
		historyProjectManager.save(historyProject);
		flowCompletionDao.save(flowCompletion);
		projectManager.saveOrupdateProj(projectInfo);
	}

	public FlowCompletion getFlowCompletion(String id) {
		return flowCompletionDao.get(id);
	}

	public Page<FlowCompletion> getFlowCompletions(
			final Page<FlowCompletion> page, final QueryFilter queryFilter) {
		return flowCompletionDao.findPage(page, queryFilter);
	}

	public FlowCompletion getFlowCompletion(ProjectInfo projectInfo) {
		return flowCompletionDao.findUniqueBy("projectInfo", projectInfo);
	}

	public CompPetition getCompPetition(String id) {
		return compPetitionDao.get(id);
	}

	public CompPetition getCompPetition(FlowCompletion flowCompletion) {
		return compPetitionDao.findUniqueBy("flowCompletion", flowCompletion);
	}

	public CompExpertise getCompExpertise(String id) {
		return compExpertiseDao.get(id);
	}

	public CompExpertise getCompExpertise(FlowCompletion flowCompletion) {
		return compExpertiseDao.findUniqueBy("flowCompletion", flowCompletion);
	}

	public CompReport getCompReport(String id) {
		return compReportDao.get(id);
	}

	public CompReport getCompReport(FlowCompletion flowCompletion) {
		return compReportDao.findUniqueBy("flowCompletion", flowCompletion);
	}

	/**
	 * 根据key查询
	 * 
	 * @param key
	 * @return
	 */
	public FlowProject getByKey(String key) {
		return flowProjectDao.findUniqueBy("key", key);
	}
}
